package com.example.tvidemo.controller;

import com.example.tvidemo.entity.Role;
import com.example.tvidemo.rest.ResponseData;
import com.example.tvidemo.service.RoleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation(value="获取所有角色详细信息", notes="获取所有角色详细信息")
    @GetMapping("/noaccess/findAll")
    public ResponseData findAll(){
        List<Role> roles = roleService.findAll();
        return new ResponseData(200,"请求成功",roles);
    }

    @ApiOperation(value="获取角色详细信息", notes="根据url的id来获取角色详细信息")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "String",paramType="path")
    @GetMapping("/get/{id}")
    public ResponseData getUser(@PathVariable("id") String id){
        Role role = roleService.findById(id);
        return new ResponseData(200,"请求成功",role);
    }

    @ApiOperation(value="创建角色", notes="根据Role对象创建角色")
    @ApiImplicitParam(name = "role", value = "角色详细实体role", required = true, dataType = "Role")
    @PostMapping("/add")
    public ResponseData addUser(@RequestBody Role role){
        roleService.addRole(role);
        return new ResponseData(200,"添加成功",role);
    }

    @ApiOperation(value="删除角色", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "String",paramType="path")
    @DeleteMapping("/delete/{id}")
    public ResponseData delUser(@PathVariable("id") String id){
        roleService.deleteRole(id);
        return new ResponseData(200,"删除成功",null);
    }

    @ApiOperation(value="更新角色详细信息", notes="根据url的id来指定更新对象，并根据传过来的role信息来更新角色详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "String",paramType="path"),
            @ApiImplicitParam(name = "role", value = "用户详细实体role", required = true, dataType = "Role")
    })
    @PutMapping("/update/{id}")
    public ResponseData updateUser(@PathVariable("id") String id,@RequestBody Role role){
        Role updateRole = roleService.findById(id);
        updateRole.setRoleName(role.getRoleName());
        updateRole.setRoleEnname(role.getRoleName());
        return new ResponseData(200,"修改成功",null);
    }

}
